import { stringify } from "querystring"
import * as Redis from 'ioredis'
import {redisConfig} from '../config'

const redis = new Redis({
    port: redisConfig.port,
    password: redisConfig.password,
    host:redisConfig.host
})
/**
 * 
 * @param key 
 * @param value 
 * @param seconds 过期时间 秒 
 */
export async function setExpire(key:string,value:string,seconds:number){
    try {
        await redis.setex(key,seconds,value)    
    } catch (error) {
        return Promise.reject(error)
    }
}
export async function getObject<T>(key:string) {
    try {
        let value:any= await redis.get(key)
        let object = JSON.parse(value)
        return object as T
    } catch (error) {
        console.log(error)
        return Promise.reject(error)
    }
}
export default redis